
#include <BLine>
#include "member_BSurface.h"

using namespace BWE;

member_BSurface::member_BSurface(BSurface* surface)
{
	boss = surface;
	orientation = Orientation_Horizontal;
	horizontalSegment = 2;
	verticalSegment = 2;
	dirty = true;
}
member_BSurface::~member_BSurface()
{

}

bool member_BSurface::rebuildHorizontal()
{
	int count = (horizontalSegment + 1) * (verticalSegment + 1);
	if (count == 0)
	{
		boss->clear();
		return true;
	}

	BVector normal = quad.normal();

	if (!vertices || vertices->size() != count)
	{
		vertices = new BVectorArray(count);
		normals = new BVectorArray(count);
		coords = new BCoordArray(count);
	}
	
	BLine lineAD(quad.a(), quad.d());
	BLine lineBC(quad.b(), quad.c());
	BReal stepV = 1.0 / verticalSegment;
	BReal stepH = 1.0 / horizontalSegment;
	for (int r = 0; r < verticalSegment + 1; r++)
	{
		BReal ratioV = r * stepV;
		BLine line(lineAD.vertex(ratioV), lineBC.vertex(ratioV));
		int head = r * (horizontalSegment + 1);
		for (int c = 0; c < horizontalSegment + 1; c++)
		{
			vertices->set(head + c, line.vertex(c * stepH));
			(*coords)[head + c].set(float(ratioV), float(c * stepH));
		}
	}
	normals->fill(normal);

	indices = new BIndexArray(verticalSegment * horizontalSegment * 4);
	int fi = 0;
	for (int r = 0; r < verticalSegment; r++)
	{
		int head = r * (horizontalSegment + 1);
		int pos = r * horizontalSegment * 4;
		for (int c = 0; c < horizontalSegment; c++)
		{
			int i0 = head + c;
			int i1 = i0 + 1;
			int i2 = i1 + (horizontalSegment + 1);
			int i3 = i0 + (horizontalSegment + 1);
			(*indices)[pos + c * 4] = i0;
			(*indices)[pos + c * 4 + 1] = i1;
			(*indices)[pos + c * 4 + 2] = i2;
			(*indices)[pos + c * 4 + 3] = i3;
		}
	}

	return true;
}
bool member_BSurface::rebuildVertical()
{
	int count = (horizontalSegment + 1) * (verticalSegment + 1);
	if (count == 0)
	{
		boss->clear();
		return true;
	}

	BVector normal = quad.normal();

	if (!vertices || vertices->size() != count)
	{
		vertices = new BVectorArray(count);
		normals = new BVectorArray(count);
		coords = new BCoordArray(count);
	}

	BLine lineAB(quad.a(), quad.b());
	BLine lineDC(quad.d(), quad.c());
	float stepV = 1.0f / verticalSegment;
	float stepH = 1.0f / horizontalSegment;
	for (int r = 0; r < horizontalSegment + 1; r++)
	{
		float ratioH = r * stepH;
		BLine line(lineAB.vertex(ratioH), lineDC.vertex(ratioH));
		int head = r * (verticalSegment + 1);
		for (int c = 0; c < verticalSegment + 1; c++)
		{
			vertices->set(head + c, line.vertex(c * stepV));
			(*coords)[head + c].set(c * stepV, ratioH);
		}
	}
	normals->fill(normal);

	indices = new BIndexArray(verticalSegment * horizontalSegment * 4);
	int fi = 0;
	for (int r = 0; r < horizontalSegment; r++)
	{
		int head = r * (verticalSegment + 1);
		int pos = r * verticalSegment * 4;
		for (int c = 0; c < verticalSegment; c++)
		{
			int i0 = head + c;
			int i1 = i0 + (verticalSegment + 1);
			int i2 = i1 + 1;
			int i3 = i0 + 1;
			(*indices)[pos + c * 4] = i0;
			(*indices)[pos + c * 4 + 1] = i1;
			(*indices)[pos + c * 4 + 2] = i2;
			(*indices)[pos + c * 4 + 3] = i3;
		}
	}

	return true;
}
